package com.example.dblibrary.app.ImplMvp;


import android.annotation.SuppressLint;
import android.arch.lifecycle.LifecycleOwner;
import android.support.annotation.NonNull;
import android.util.Log;

import com.example.dblibrary.AppApplication;
import com.example.dblibrary.app.BaseActivity;
import com.example.dblibrary.database.AppDatabase;
import com.example.dblibrary.mvp.BaseMvp.BaseContract;


public abstract class ImpActivityBasePresenter<M extends BaseContract.BaseModel,V extends BaseContract.BaseView> implements BaseContract.BasePresenter {

    protected BaseActivity context;
    protected AppDatabase mAppDatabase;
    protected M mModel;
    protected V mView;

    public ImpActivityBasePresenter(BaseActivity context) {
        this.context = context;
    }

    @Override
    public void onCreate(@NonNull LifecycleOwner owner) {
		mAppDatabase = ((AppApplication)context.getApplication()).getAppDatabase();
        mModel=bindModel();
        initData();
    }

    @SuppressLint("LongLogTag")
    @Override
    public void onResume(@NonNull LifecycleOwner owner) {

        Log.e("ImpActivityBasePresenter","onResume");

    }

    @SuppressLint("LongLogTag")
    @Override
    public void onPause(@NonNull LifecycleOwner owner) {
        Log.e("ImpActivityBasePresenter","onPause");
    }

    @SuppressLint("LongLogTag")
    @Override
    public void onStop(@NonNull LifecycleOwner owner) {
        Log.e("ImpActivityBasePresenter","onStop");
    }

    @SuppressLint("LongLogTag")
    @Override
    public void onDestroy(@NonNull LifecycleOwner owner) {
        Log.e("ImpActivityBasePresenter","onDestroy");
        unbindModel(mModel);
        dettachView(mView);
    }


    /**
     * 绑定数据模型
     *
     * @return
     */
    protected abstract M bindModel();

    public abstract void attachView(V view);

    protected abstract void unbindModel(M model);

    protected abstract void dettachView(V view);

    /**
     * 初始化数据
     */
    protected abstract void initData();

}
